#include "GameMode.h"
#include "../Game.h"
#include "../SGD Wrappers\SGD_InputManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "GameState.h"
#include "MainMenuState.h"
#include "CharSelect.h"
#include "PracticeMode.h"
#include "DodgeBomb.h"
#include "MiniGameSelect.h"

CGameMode* CGameMode::GetInstance(void)
{
	static CGameMode s_Instance;
	return &s_Instance;
}

void CGameMode::Enter(void)
{
	SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetBoomSound(), false);
	button = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/1407_IForgot_blankbutton.png");
	ball = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ball.png");
	boom = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/1407_IForgot_transistionscreen.png");

}

void CGameMode::Exit(void)
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ball);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(button);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(boom);
	flash = true;
	time = 0.0f;
}

bool CGameMode::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	if (pInput->IsKeyPressed(SGD::Key::Escape) == true || pInput->IsButtonPressed(0, 1) == true)
	{
		Game::GetInstance()->ChangeState(CMainMenuState::GetInstance());
		return true;
	}

	if (pInput->IsKeyPressed(SGD::Key::Down) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Down) == true
		)
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > 1)
			m_nCursor = 0;
		SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetMenusound(), false);

	}
	else if (pInput->IsKeyPressed(SGD::Key::Up) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Up) == true
		)
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = 1;
		SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetMenusound(), false);

	}

	if (pInput->IsKeyPressed(SGD::Key::Enter) == true || pInput->IsButtonPressed(0, 0))
	{
		if (m_nCursor == 0)
		{
			Game::GetInstance()->ChangeState(CCharSelect::GetInstance());
			return true;
		}
		else if (m_nCursor == 1)
		{
			Game::GetInstance()->ChangeState(CPracticeMode::GetInstance());
			return true;
		}
		else if (m_nCursor == 2)
		{
			Game::GetInstance()->ChangeState(CMiniGameSelect::GetInstance());
			return true;
		}		
	}

	return true;
}

void CGameMode::Update(float elapsedTime)
{
	
	if (time > 1.2f)
		flash = false;

	time += elapsedTime;

}

void CGameMode::Render(void)
{
	float width = Game::GetInstance()->GetScreenWidth();
	const Font* pFont = Game::GetInstance()->GetFont();
	
	Game* pGame = Game::GetInstance();

	float darkOffset = pGame->DarkOff();
	float medOffset = pGame->MedOff();
	float lightOffset = pGame->LightOff();

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset - 805, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset - 805, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset - 805, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	if (flash)	
		SGD::GraphicsManager::GetInstance()->DrawTexture(boom, { 0, 0 }, 0.0f, {}, { 255, 255, 255 }, { 1, 1 });
	else
	{
		pFont->Draw("Game Select", { 1, 1 }, 1, { 255, 255, 255 });

		SGD::GraphicsManager::GetInstance()->DrawTexture(button, { 360, 80 }, 0.0f, {}, { 255, 255, 255 }, { 0.8f, 0.8f });
		pFont->Draw("Play", { (width - (4 * 32)) / 2, 140 },
			1, { 0, 255, 0 });

		SGD::GraphicsManager::GetInstance()->DrawTexture(button, { 360, 250 }, 0.0f, {}, { 255, 255, 255 }, { 0.8f, 0.8f });
		pFont->Draw("Practice", { (width - (4 * 32)) / 2.7f, 330 },
			1, { 0, 0, 255 });

		//SGD::GraphicsManager::GetInstance()->DrawTexture(button, { 360, 440 }, 0.0f, {}, { 255, 255, 255 }, { 0.8f, 0.8f });
	//	pFont->Draw("Mini Game", { (width - (4 * 32)) / 2.7f, 530 },
			//0.7f, { 255, 255, 255 });

		SGD::GraphicsManager::GetInstance()->DrawTexture(ball, { (width - (4 * 32)) / 4.5f, 130.0f + 200 * m_nCursor }, 0.0f, {}, {}, { 0.25f, 0.25f });
	}
	
}

